﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;

namespace Marmat.Forms.Skin
{
    /// <summary>
    /// 基于矩形的扩展实例
    /// </summary>
    public static class RectangleExtension
    {

        #region 获取矩形的圆角路径
        /// <summary>
        /// 获取矩形适当的圆角路径。
        /// </summary>
        /// <param name="rect">矩形对象</param>
        /// <returns></returns>
        public static GraphicsPath GetRoundPath(this Rectangle rect)
        {
            float round = Math.Min(rect.Width, rect.Height) / 2;
            return GetRoundPath(rect, round);
        }

        /// <summary>
        /// 获取矩形的圆角路径。
        /// </summary>
        /// <param name="rect">矩形对象</param>
        /// <param name="round">圆角大小</param>
        /// <returns></returns>
        public static GraphicsPath GetRoundPath(this Rectangle rect, float round)
        {
            return GetRoundPath(rect, round, round, round, round);
        }

        /// <summary>
        /// 获取矩形的圆角路径。
        /// </summary>
        /// <param name="rect">矩形对象</param>
        /// <param name="round">圆角大小</param>
        /// <returns></returns>
        public static GraphicsPath GetRoundPath(this Rectangle rect, int round)
        {
            return GetRoundPath(rect, round, round, round, round);
        }

        /// <summary>
        /// 获取矩形的圆角路径。
        /// </summary>
        /// <param name="rect">矩形对象</param>
        /// <param name="leftTop">左上角的矩形对象</param>
        /// <param name="rightTop">右上角的的圆角大小</param>
        /// <param name="leftBottom">左下角的圆角大小</param>
        /// <param name="rightBottom">右下角的圆角大小</param>
        /// <returns></returns>
        public static GraphicsPath GetRoundPath(this Rectangle rect, int leftTop, int rightTop, int leftBottom, int rightBottom)
        {
            return GetRoundPath(rect, (float)leftTop, rightTop, leftBottom, rightBottom);
        }

        /// <summary>
        /// 获取矩形的圆角路径。
        /// </summary>
        /// <param name="rect">矩形对象</param>
        /// <param name="leftTop">左上角的矩形对象</param>
        /// <param name="rightTop">右上角的的圆角大小</param>
        /// <param name="leftBottom">左下角的圆角大小</param>
        /// <param name="rightBottom">右下角的圆角大小</param>
        /// <returns></returns>
        public static GraphicsPath GetRoundPath(this Rectangle rect, float leftTop, float rightTop, float leftBottom, float rightBottom)
        {
            GraphicsPath path = new GraphicsPath();
            if (rect.Width == 0 || rect.Height == 0)
                throw new ArgumentException();
            else if (leftTop == 0 && rightTop == 0 && leftBottom == 0 && rightBottom == 0)
            {
                path.AddRectangle(rect);
            }
            else
            {
                PointF start_point;
                PointF end_point;
                // 左上圆角
                if (leftTop > 0)
                    path.AddArc(rect.Left, rect.Top, leftTop * 2, leftTop * 2, 180, 90);

                // 顶部线条
                start_point = new PointF(rect.Left + leftTop * 2, rect.Top);
                end_point = new PointF(rect.Right - rightTop * 2, rect.Top);
                if (start_point.X < end_point.X)
                    path.AddLine(start_point, end_point);

                // 右上圆角
                if (rightTop > 0)
                    path.AddArc(rect.Right - rightTop * 2, rect.Top, rightTop * 2, rightTop * 2, -90, 90);

                // 右侧线条
                start_point = new PointF(rect.Right, rect.Top + rightTop * 2);
                end_point = new PointF(rect.Right, rect.Bottom - rightBottom * 2);
                if (start_point.Y < end_point.Y)
                    path.AddLine(start_point, end_point);

                // 右下圆角
                if (rightBottom > 0)
                    path.AddArc(rect.Right - rightBottom * 2, rect.Bottom - rightBottom * 2, rightBottom * 2, rightBottom * 2, 0, 90);

                // 底部线条
                start_point = new PointF(rect.Right - rightBottom * 2, rect.Bottom);
                end_point = new PointF(rect.Left + leftBottom * 2, rect.Bottom);
                if (start_point.X > end_point.X)
                    path.AddLine(start_point, end_point);

                //左下圆角
                if (leftBottom > 0)
                    path.AddArc(rect.Left, rect.Bottom - leftBottom * 2, leftBottom * 2, leftBottom * 2, 90, 90);

                // 左侧线条
                start_point = new PointF(rect.Left, rect.Bottom - leftBottom * 2);
                end_point = new PointF(rect.Left, rect.Top + leftTop);
                if (start_point.Y > end_point.Y)
                    path.AddLine(start_point, end_point);

                path.CloseFigure();
            }
            return path;
        }
        #endregion

    }
}
